package indi.zhifa.recipe.bailan5.gateway.controller.api;

import indi.zhifa.recipe.bailan.framework.common.entity.annotations.ZfRestController;
import indi.zhifa.recipe.bailan.framework.common.entity.dto.RestResponse;
import io.netty.handler.codec.http.HttpResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

import java.math.BigInteger;
import java.util.concurrent.CompletableFuture;

@Slf4j
@RequestMapping("/api/test")
@RestController
public class HelloController {

    @GetMapping("/hello")
    Mono<RestResponse<String>> hello(){
        RestResponse<String> r = RestResponse.ok("hello");
        return Mono.just(r);
    }

    BigInteger getFibonacii(int n){
        BigInteger n1 = new BigInteger("1");
        BigInteger n2 = new BigInteger("1");
        if(n==1){
            return n1;
        }else if(n==2){
            return n2;
        }else {
            BigInteger res = new BigInteger("1");
            for(int i=3;i<=n;i++){
                res = n1.add(n2);
                n1 = n2;
                n2 = res;
            }
            return res;
        }
    }

    @GetMapping("/fibonacii/{n}")
    Mono<RestResponse<BigInteger>> fibonacii(@PathVariable(name = "n") int n){
        CompletableFuture<RestResponse<BigInteger>> future = CompletableFuture.supplyAsync(()->{
            log.info("计算"+n+" 当前线程 "+Thread.currentThread().getId());
            BigInteger res =  getFibonacii(n);
            log.info("计算"+n+" 当前线程 "+Thread.currentThread().getId()+"已完成===========================");
            return RestResponse.ok(res);});
        log.info("^_^返回"+n+" 当前线程 "+Thread.currentThread().getId());
        return Mono.fromFuture(future);
    }




}
